package ag.jumpguy;

import java.math.BigDecimal;

import ag.core.AGEngine;
import android.graphics.PointF;
import android.util.Log;

public class JGObject {
	public PointF pos;
	private PointF originPos;
	public Vector _vector;
	public VelocityVector _velocityVector;
	private float _time;
	private float _size;

	public JGObject(PointF pos, Vector vector, float v) {
		this._vector = vector;
		this.originPos = new PointF(pos.x, pos.y);
		this.originPos.x = new BigDecimal(this.originPos.x).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
		this.originPos.y = new BigDecimal(this.originPos.y).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
		this.pos = new PointF(this.originPos.x, this.originPos.y);
		this._velocityVector = new VelocityVector(this._vector, v);
		this._size = 30;
	}

	public void reset(PointF originPos, Vector vector, float v) {
		Log.d("ag-wave",
				String.format("reset x:%s y:%s,", originPos.x, originPos.y));
		this.originPos.x = originPos.x;
		this.originPos.y = originPos.y;
		this.originPos.x = new BigDecimal(this.originPos.x).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
		this.originPos.y = new BigDecimal(this.originPos.y).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
		
		this._vector = vector;
		this._velocityVector = new VelocityVector(vector, v);
		this._time = 0;
	}

	public void onUpdate(AGEngine engine) {
		this._time += 0.3f;
		float xs = this._velocityVector.getXS(this._time);
		float ys = this._velocityVector.getYS(this._time);
		xs = new BigDecimal(xs).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
		ys = new BigDecimal(ys).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
		this.pos.x = this.originPos.x + xs;
		this.pos.y = this.originPos.y + ys;// this.v * this.time;//vs;
		
		this.pos.x = new BigDecimal(this.pos.x).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
		this.pos.y = new BigDecimal(this.pos.y).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
	}


	public void onRender(AGEngine engine, GameAcoordinateSystem gas) {
		PointF posInScreen = gas.toScreen(this.pos);
		engine.graphics.drawCircle(posInScreen, this._size,
				engine.graphics.GreenTextPaint);

			PointF posInScreen2 = gas.toScreen(new PointF(this.pos.x,
					this.pos.y + this._velocityVector.getYS(10)));
			PointF posInScreen3 = gas.toScreen(new PointF(this.pos.x
					+ this._velocityVector.getXS(10), this.pos.y));
			engine.graphics.drawLine(posInScreen.x, posInScreen.y,
					posInScreen2.x, posInScreen2.y,
					engine.graphics.BlueTextPaint);
			engine.graphics.drawLine(posInScreen.x, posInScreen.y,
					posInScreen3.x, posInScreen3.y,
					engine.graphics.BlueTextPaint);
		
			PointF delta = this._vector.forward(100.0f);
			PointF sPos = new PointF(this.pos.x +10, this.pos.y + 10);
			PointF sPosInScreen = gas.toScreen(sPos);
			PointF posInScreen4 = gas.toScreen(new PointF(sPos.x,
					sPos.y + delta.y));
			PointF posInScreen5 = gas.toScreen(new PointF(sPos.x + delta.x,
					sPos.y));
			engine.graphics.drawLine(sPosInScreen.x, sPosInScreen.y,
					posInScreen4.x, posInScreen4.y,
					engine.graphics.RedTextPaint);
			engine.graphics.drawLine(sPosInScreen.x, sPosInScreen.y,
					posInScreen5.x, posInScreen5.y,
					engine.graphics.RedTextPaint);
			engine.graphics.drawText(String.format("x:%s", this.pos.x),20,140, engine.graphics.DebugTextPaint);
			engine.graphics.drawText(String.format("y:%s", this.pos.y),20,160, engine.graphics.DebugTextPaint);			
		engine.graphics.drawText(String.format("%s %s",this._velocityVector.vX, this._velocityVector.vY),20,180, engine.graphics.DebugTextPaint);
		// this._vector.vectorX, this._vector.vectorY, this._vector.vX,
		// this._vector.vY), 20,180, engine.graphics.DebugTextPaint);

	}
	
	public Vector getCurrentVector(){
		PointF featurePos = new PointF(this.pos.x + this._velocityVector.getXS(10),
				this.pos.y + this._velocityVector.getYS(10));
		return new Vector(this.pos.x, this.pos.y, featurePos.x, featurePos.y);
	}
}
